Using the :not() Pseudo-Class in CSS
The :not() pseudo-class in CSS allows you to exclude specific elements from a set of selectors. This helps you apply styles broadly while intentionally leaving out certain elements.
:not(selector) selects all elements that do not match the specified selector.
You can combine :not() with other pseudo-classes and type selectors for precise targeting.
It is especially useful to reduce redundant classes or prevent overriding styles unintentionally.
In this example, all buttons except the disabled one are styled with a blue background and white text. The disabled button is excluded from the hover and base styles automatically using :not(:disabled).
Use :not() to simplify CSS and avoid extra classes for exclusions.
Combine :not() with pseudo-classes like :hover, :focus, or :checked for dynamic styling.
Avoid overly complex nested :not() expressions, as they may reduce readability and performance.
Test across browsers to ensure consistent exclusion behavior.
You're trying to style all buttons except the ones with class 'disabled', but the disabled buttons are still getting the styles. How would you fix your CSS selector?
You have a list of cards and want to apply a border to all except the first one. How would you write the CSS using :not()?
What happens if you write :not(.btn):not(.primary) — does that exclude elements that have either class or both?
A feature team reports that some dynamically loaded modals are getting unintended styles even though they have a 'modal' class — your CSS uses :not(.modal) on a parent container. What could be going wrong, and how would you debug it?
You're refactoring a legacy UI and notice that :not([data-hidden]) is applied to hundreds of elements. The page feels sluggish on mobile. What performance concerns might this introduce, and how would you optimize it?
A designer wants all form inputs styled except those inside a 'readonly-section'. But the section is added dynamically via JS. Why might :not(.readonly-section input) fail, and what’s a more robust approach?
You're designing a reusable component library where :not() is used to exclude variants from base styles. How do you ensure this doesn't create specificity wars when consumers override styles, and what alternatives would you consider?
In a large-scale app with 10k+ DOM nodes, you're using :not([data-state='loading']) on every list item. How would you evaluate the performance impact, and what architectural changes might you propose?
A team uses :not() extensively to avoid CSS class bloat, but now their stylesheet is hard to maintain. How would you balance the tradeoff between semantic cleanliness and selector complexity in a scalable design system?
You're leading a migration from a legacy CSS framework that uses inline styles and JS-based exclusions to a modern CSS-in-JS system. How would you handle the deprecation of widespread :not() usage without breaking existing layouts or introducing performance regressions?
Your company's design system uses :not() to conditionally apply theme tokens across components. As you scale to 50+ products with different theming rules, how do you architect this to avoid combinatorial explosion and ensure consistent behavior across teams?
A cross-team initiative wants to standardize CSS exclusion logic. Some teams use :not(), others use !important overrides or JS classes. How would you design a governance model and migration path that balances flexibility, performance, and long-term maintainability?